#
# DailyLiveSnap.Command
#

COMMANDS = [
    'create', 'first', 'help', 'info', 'last', 'log', 'next',
    'previous', 'prune', 'sync',
]

ALIASES = {
    'init':     'create',
    'n':        'next',
    'p':        'previous',
    'prev':     'previous',
    'rm':       'prune',
    'remove':   'prune',
    'snap':     'sync',
    'up':       'sync',
}


class Command(object):

    def __init__(self):
        pass

    @staticmethod
    def parse_options(args):
        raise NotImplementedError

    def __call__(self, options):
        raise NotImplementedError


def load_command(cmdname):
    cmd = ALIASES.get(cmdname, cmdname)
    if cmd not in COMMANDS:
        raise ValueError, "`%s' is not a valid command." % cmd

    modname = 'cmd_%s' % cmd
    mod = getattr(__import__('DailyLiveSnap.%s' % modname), modname)
    for name, prop in mod.__dict__.iteritems():
        if (prop is not Command and type(prop) == type(Command)
                and issubclass(prop, Command)):
            return prop

    raise ValueError, "No command handler found in `%s' command.'" % cmd
